<?php 

define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require('./classes/control/control_new_room.php');

class New_Room
{
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	public function __construct()
	{
		$this->new_room_c = new NewRoomControl();
	}
	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{
	
		$url_info = $new_info = $category_info = array();
		//分页处理
		$page = !empty($_REQUEST['page'])?(int)$_REQUEST['page']: 1;
		$page_size = 20;
		$new_title = !empty($_POST['new_title'])?trim($_POST['new_title']):urldecode(trim($_GET['new_title']));
		if($_POST['search'] || !empty($_REQUEST['url_page']) )//搜索以及分页
		{
			$search_array = array
			(
			'created_date' => trim($_REQUEST['created_date']),
			'new_id' => trim($_REQUEST['new_id']),
			'title' => $new_title
			);
			$url_info = $this->new_room_c->get_url_info($page,'list_s',$page_size,$search_array);
			$new_info = $this->new_room_c->get_new_info('list_s',$url_info['page_info']['start'],$page_size,$search_array);
		}else
		{
			$url_info = $this->new_room_c->get_url_info($page,'list',$page_size);
			$new_info = $this->new_room_c->get_new_info('list',$url_info['page_info']['start'],$page_size);
		}
		
		$GLOBALS['smarty']->assign('new_id',      trim($_REQUEST['new_id']));
		$GLOBALS['smarty']->assign('new_title',   $_REQUEST['new_title']);
		$GLOBALS['smarty']->assign('created_date',   $_REQUEST['created_date']);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('new_info',$new_info );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('new_room_index.htm'); 
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	
	public function edit()
	{	
		$new_info = array();	
		if($_POST['sub_from'])//修改或者增加
		{
			$array = array
			(
			'title' =>  trim($_POST['new_title']),
			'sub_title' =>  trim($_POST['new_sub_title']),
			'keyword' =>  trim($_POST['new_keyword']),
			'summary' => trim($_POST['new_summary']),
			'content' => trim($_POST['new_content']),
			'id' => (int)$_POST['new_id']
			);
			if($_REQUEST['act'] == 'add')
			{
				$last_id  = $this->new_room_c->operate_new($_REQUEST['act'],$array);
				if(!empty($last_id))
				{
					echo  '<script type="text/javascript"> alert("增加成功！");
					window.location.href="/wondershare/news_room.php?action=edit&act=update&new_id='.(int)$last_id.'"; </script>';
					
				}
			}else
			{
				$affect = $this->new_room_c->operate_new($_REQUEST['act'],$array);
				if(!empty($affect))
				{
					echo  '<script type="text/javascript"> alert("编辑成功！");
					window.location.href="/wondershare/news_room.php?action=edit&act=update&new_id='.(int)$_POST['new_id'].'"; </script>';
				}
			}
			exit;
		}
		if($_REQUEST['act'] == 'add')//增加
		{
			$GLOBALS['smarty']->assign('title',     '添加');
			$GLOBALS['smarty']->assign('form_act',     $_REQUEST['act']);  
		}elseif($_REQUEST['act'] == 'update')//修改
		{
			$new_info = $this->new_room_c->get_new_id_info((int)$_REQUEST['new_id']);
			$GLOBALS['smarty']->assign('title','修改');
			$GLOBALS['smarty']->assign('form_act',$_REQUEST['act']);
		}
		$GLOBALS['smarty']->assign('new_info',$new_info);
		$GLOBALS['smarty']->display('new_room_info.htm'); 
	
	}
	/**
	* @函数del
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->new_room_c->delete_new($checkList);
		}
	}
	/**
	* 处理相关的ajax
	* @param  NULL
	* @return  string
	*/
	function  process_ajax()
	{
		switch ($_GET['type'])
		{
			case 'new_sort'://修改排序
			if(!empty($_GET['new_id']))
			$this->new_room_c->update_info((int)$_GET['new_id'],$_GET['value'],'order_id');
			break;
		}
	}



}

$action    = $_REQUEST['action']; 
$new_room   = new New_Room;
if(method_exists($new_room, $action)) 
{
	$new_room->$action();
}else{
	$new_room->index();
}

?>
